package com.ruoyi.mongo.senior.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.mongo.comprehensive.SeniorChinToEng;
import com.ruoyi.mongo.service.MongoConstants;
import com.ruoyi.mongo.senior.entity.Senior;
import com.ruoyi.mongo.senior.entity.SeniorNet;
import com.ruoyi.mongo.senior.entity.SeniorRecord;
import com.ruoyi.mongo.senior.service.SeniorInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

@Slf4j
@Service
public class SeniorService implements SeniorInterface {

    private final MongoTemplate comprehensiveTemplate;

    public SeniorService(@Qualifier("comprehensiveTemplate") MongoTemplate comprehensiveTemplate) {
        this.comprehensiveTemplate = comprehensiveTemplate;
    }

    @Override
    public List<Senior> selectListByGroup(int group) {
        Criteria criteria;
        Query query = new Query();
        if (group != 0) {
            criteria = Criteria.where(MongoConstants.MONGO_GROUP).is(group);
            query.addCriteria(criteria);
        } else {
            query.limit(MongoConstants.MONGO_MAX_GROUP_SIZE);
        }
        return comprehensiveTemplate.find(query, Senior.class);
    }

    @Override
    public List<SeniorRecord> selectTestListByPractice(String practice) {
        Long userId = getUserId();
        String collectionName = MongoConstants.COLLECT_SENIOR_TEST + "_" + userId;
        return selectListDetailByPractice(practice, collectionName);
    }

    @Override
    public List<SeniorRecord> selectStudyDetailByPractice(String practice) {
        Long userId = getUserId();
        return selectStudyDetailByPractice(practice,userId);
    }

    @Override
    public List<SeniorRecord> selectStudyDetailByPractice(String practice, Long userId) {
        String collectionName = MongoConstants.COLLECT_SENIOR_STUDY + "_" + userId;
        return selectListDetailByPractice(practice, collectionName);
    }

    public List<SeniorRecord> selectListDetailByPractice(String practice, String collectionName) {
        Query query = getPracticeQuery(practice);
        return comprehensiveTemplate.find(query, SeniorRecord.class, collectionName);
    }

    public static Query getPracticeQuery(String practice) {
        Query query = new Query();
        if (practice != null && !practice.isEmpty()) {
            query.addCriteria(Criteria.where(MongoConstants.MONGO_PRACTICE).is(practice));
        }
        return query;
    }

    @Override
    public List<Senior> selectListOnlyOneMeaning(int group) {
        List<Senior> list = selectListByGroup(group);
        list.forEach(senior -> {
            String chinese = senior.getChinese();
            if (chinese.contains(MongoConstants.MONGO_SYMBOL_COMMA)) {
                String[] chineseArray = chinese.split(MongoConstants.MONGO_SYMBOL_COMMA);
                Random random = new Random();
                int chineseIndex = random.nextInt(chineseArray.length);
                senior.setChinese(chineseArray[chineseIndex]);
            }
        });
        return list;
    }

    @Override
    public List<SeniorRecord> selectListByPracticeId(String practice) {
        Long userId = getUserId();
        return selectListByPracticeId(practice, userId);
    }

    @Override
    public List<SeniorRecord> selectListByPracticeId(String practice, long userId) {
        Query query = getPracticeQuery(practice);
        String collectionName = MongoConstants.COLLECT_SENIOR_TEST + "_" + userId;
        return comprehensiveTemplate.find(query, SeniorRecord.class, collectionName);
    }

    @Override
    public List<Senior> selectListByType(String type, int limit) {
        return List.of();
    }

    @Override
    public List<Senior> selectListByTypeRandom(String type, int limit) {
        return List.of();
    }

    @Override
    public void saveData(Senior entity) {
        Long userId = getUserId();
        saveData(entity, userId);
    }

    @Override
    public void saveData(List<Senior> entitylist) {
        Long userId = getUserId();
        entitylist.forEach(entity -> {
            saveData(entity, userId);
        });
    }

    @Override
    public void saveTestData(SeniorRecord entity) {
        Long userId = getUserId();
        saveTestData(entity, userId);
    }

    @Override
    public void saveTestData(List<SeniorRecord> entitylist) {
        Long userId = getUserId();
        entitylist.forEach(entity -> {
            saveTestData(entity, userId);
        });
    }

    @Override
    public void saveTestData(SeniorRecord entity, Long userId) {
        entity.setTime(DateUtils.getLocalTime());
        entity.set_id(null);
        if (entity.getSelected() != null) {
            String chinese = entity.getChinese().replaceAll("[^a-zA-Z0-9\u4e00-\u9fa5]", "");
            String select = entity.getSelected().replaceAll("[^a-zA-Z0-9\u4e00-\u9fa5]", "");
            if (chinese.trim().equals(select.trim())) {
                entity.setResult(true);
            }
        }
        comprehensiveTemplate.save(entity, MongoConstants.COLLECT_SENIOR_TEST + "_" + userId);
    }

    @Override
    public void saveData(Senior entity, Long userId) {
        entity.setTime(DateUtils.getLocalTime());
        entity.set_id(null);
        comprehensiveTemplate.save(entity, MongoConstants.COLLECT_SENIOR + "_" + userId);
    }

    @Override
    public void insertNetDataByUser(Senior entity) {
        Long userId = getUserId();
        insertNetDataByUser(entity, userId);
    }

    @Override
    public void saveWriteData(SeniorChinToEng entity) {
        comprehensiveTemplate.save(entity);
    }

    @Override
    public void saveWriteData(SeniorChinToEng entity, String collectionName) {
        Long userId = getUserId();
        entity.setTime(DateUtils.getLocalTime());
        entity.set_id(null);
        String collection = collectionName + "_" + userId;
        comprehensiveTemplate.save(entity, collection);
    }

    @Override
    public List<SeniorChinToEng> selectWriteListByPractice(String practice) {
        Long userId = getUserId();
        String collectionName = MongoConstants.COLLECT_SENIOR_CHIN_TO_ENG + "_" + userId;
        return selectWriteListByPractice(practice, collectionName);
    }

    @Override
    public List<SeniorChinToEng> selectWriteListByPractice(String practice, String collectionName) {
        Query query = getPracticeQuery(practice);
        return comprehensiveTemplate.find(query, SeniorChinToEng.class, collectionName);
    }


    @Override
    public void insertNetDataByUser(Senior entity, Long userId) {
        if (entity.getWordMore() != null && !entity.getWordMore().isEmpty()) {
            log.info("保存网络资源:{},时间:{}", entity.getName(), DateUtils.getLocalTime());
            SeniorNet seniorNet = new SeniorNet();
            BeanUtils.copyBeanProp(seniorNet, entity);
            seniorNet.setUserId(userId);
            comprehensiveTemplate.save(seniorNet);
        }
    }
}
